<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="注册号" prop="registerCode">
        <el-input v-model="queryParams.registerCode" placeholder="请输入注册号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="studentName">
        <el-input v-model="queryParams.studentName" placeholder="请输入姓名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="院部" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择院部" clearable size="small" @change="handSpeciality">
          <el-option v-for="item in deptOptions" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
        </el-select>
      </el-form-item> 
      <el-form-item label="专业" prop="specialityId">
        <el-select v-model="queryParams.specialityId" placeholder="请选择专业" clearable size="small" @change="changeGrade">
          <el-option v-for="item in specialityList" :key="item.VALUE" :label="item.TITILE" :value="item.VALUE" />
        </el-select>
      </el-form-item>
      <el-form-item label="年级" prop="gradeId">
        <el-select v-model="queryParams.gradeId" placeholder="请选择年级" clearable size="small" @change="changeGrade">
          <el-option v-for="item in gradeDate" :key="item.VALUE" :label="item.TITILE" :value="item.VALUE" />
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择班级" clearable size="small">
          <el-option v-for="item in classListType" :key="item.VALUE" :label="item.TITLE" :value="item.VALUE" />
        </el-select>
      </el-form-item>
      <el-form-item label="楼号" prop="houseId">
        <el-select v-model="queryParams.houseId" placeholder="请选择楼号" clearable size="small">
          <el-option v-for="item in houseList" :key="item.houseId" :label="item.houseName" :value="item.houseId" />
        </el-select>
      </el-form-item>
      <el-form-item label="违纪类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择违纪类型" clearable size="small">
          <el-option v-for="dict in dict.type.dormitory_violation_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['dormitory:discipline:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['dormitory:discipline:remove']">删除</el-button>
      </el-col>
    </el-row>


    <el-table v-loading="loading" :data="disciplineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="公寓楼" align="center" prop="houseNo" />
      <el-table-column label="楼层" align="center" prop="floorNo" />
      <el-table-column label="房间号" align="center" prop="roomNo" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="违纪类型" align="center" prop="violateType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dorm_violate_type" :value="scope.row.violateType" />
        </template>
      </el-table-column>
      <el-table-column label="违纪时间" align="center" prop="violateTime" width="180" />
      <el-table-column label="违纪事实" align="center" prop="violateContent" show-overflow-tooltip />
      <el-table-column label="处理结果" align="center" prop="violateResult" show-overflow-tooltip />
      <el-table-column label="处理状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ scope.row.delFlag == '0' ? '未处理' : '已删除'}}
          <!-- <dict-tag :options="[{ label: '已删除', value: '2' }, { label: '未处理', value: '0' }]" :value="scope.row.delFlag" /> -->
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="updateTime" width="180" />
      
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['dormitory:discipline:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['dormitory:discipline:remove']">删除</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <add-form ref="addForm" :title="title" :open="open" @close="cancel" @submit="submitForm" />

    <!-- 查看详情对话框 -->
    <detail ref="detail" :open="openView" @close="closeView" />
  </div>
</template>

<script>
import { listDiscipline, getDiscipline, delDiscipline, addDiscipline, updateDiscipline } from "@/api/departure/discipline";
import { getGradeSelect } from "@/api/information/gradeInfor";
import { getClassSelect } from "@/api/information/classInfor";
import { getSpecialityByDeptId } from "@/api/information/unitMajor";
import { collegeSelect } from "@/api/information/studentInfor";
import { list as houseList } from "@/api/apartment/apartmentBuilding";
import AddForm from './components/addForm';
import Detail from './components/detail';

export default {
  name: "DormitoryDiscipline",
  dicts: ['dorm_violate_type', 'dormitory_handle_status'],
  components: {
    AddForm,
    Detail
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 违纪记录表格数据
      disciplineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示查看层
      openView: false,
      // 年级数据
      gradeDate: [],
      // 班级数据
      classListType: [],
      // 院部数据
      deptOptions: [],
      // 专业数据
      specialityList: [],
      // 楼号数据
      houseList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        registerCode: undefined,
        studentName: undefined,
        type: undefined,
        gradeId: undefined,
        classId: undefined,
        deptId: undefined,
        specialityId: undefined,
        houseId: undefined
      }
    };
  },
  created() {
    this.getList();
    this.getGradeInfor();
    this.department();
    this.getHouseInfo();
  },
  methods: {
    /** 查询违纪记录列表 */
    getList() {
      this.loading = true;
      listDiscipline(this.queryParams).then(response => {
        this.disciplineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加违纪记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.violateId || this.ids;
      getDiscipline(id).then(response => {
        this.$refs.addForm.form = response.data;
        this.$refs.addForm.form.dormitoryNo = response.data.roomNo;

        this.open = true;
        this.title = "修改违纪记录";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.$refs.detail.init(row.violateId);
      this.openView = true;
    },
    /** 提交按钮 */
    submitForm(form) {
      if (form.violateId != undefined) {
        updateDiscipline(form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addDiscipline(form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.violateId || this.ids;
      this.$modal.confirm('是否确认删除违纪记录编号为"' + ids + '"的数据项？').then(() => {
        return delDiscipline(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 表单重置 */
    reset() {
      this.form = {
        violateId: undefined,
        studentNo: undefined,
        studentName: undefined,
        dormitoryNo: undefined,
        violateType: undefined,
        violateTime: undefined,
        violateContent: undefined,
        violateResult: undefined,
        remark: undefined
      };
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.violateId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
    },
    /** 关闭查看弹窗 */
    closeView() {
      this.openView = false;
    },
    // 获取年级数据
    getGradeInfor() {
      getGradeSelect().then(response => {
        this.gradeDate = response.data;
      });
    },
    // 年级变化时获取班级列表
    changeGrade(value) {
      if (value != undefined) {
        if (this.queryParams.specialityId && this.queryParams.gradeId) {
          this.getClassList();
          this.classListType = [];
          this.queryParams.classId = "";
        } else {
          if (!value) {
            this.classListType = [];
            this.queryParams.classId = "";
          }
        }
      }
    },
    // 获取班级列表
    getClassList() {
      getClassSelect({
        gradeId: this.queryParams.gradeId,
        specialtyId: this.queryParams.specialityId
      }).then(res => {
        this.classListType = res.data;
      });
    },
    // 获取院部信息
    department() {
      collegeSelect().then(res => {
        this.deptOptions = res.data;
      });
    },
    // 院部变化时获取专业列表
    async handSpeciality() {
      this.queryParams.specialityId = "";
      this.queryParams.classId = "";
      this.classListType = [];
      this.getSpeciality();
    },
    // 获取专业列表
    async getSpeciality() {
      if (this.queryParams.deptId) {
        await getSpecialityByDeptId({ deptId: this.queryParams.deptId }).then(
          (res) => {
            if (res.code == "200") {
              this.specialityList = res.data;
            } else {
              this.specialityList = [];
            }
          }
        );
      } else {
        this.specialityList = [];
      }
    },
    // 获取楼号信息
    getHouseInfo() {
      houseList({
        pageSize: 999999
      }).then(response => {
        this.houseList = response.rows;
      });
    }
  }
};
</script>